package chart;     
import java.awt.Color;   
import java.io.*;   
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpSession;   
  
import org.jfree.data.category.CategoryDataset;   
import org.jfree.data.category.DefaultCategoryDataset;   
import org.jfree.data.general.DefaultPieDataset;   
import org.jfree.data.general.PieDataset;   
import org.jfree.chart.*;   
import org.jfree.chart.entity.StandardEntityCollection;   
import org.jfree.chart.plot.*;   
import org.jfree.chart.servlet.ServletUtilities;   


// 1st , it has calculate the total number of choices. like only ABCDE, or ACDE
public class ChartImpl{   
	public static String generatePieChart(HttpSession session, PrintWriter pw,int w, int h, int pollid, int questionid){   
			String filename = null;   
			PieDataset dataset = getDataSet(pollid,questionid);
			JFreeChart chart = ChartFactory.createPieChart(   
			"Question"+questionid, 
			dataset, 
			true, 
			false, 
			false 
		);   
		chart.setBackgroundPaint(Color.GRAY);   
		try {   
			ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
			filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);   
			ChartUtilities.writeImageMap(pw, filename, info, true);   
			pw.flush();   
		} catch (IOException e) {   
			e.printStackTrace();   
		}   
		return filename;   
	}   

	public static String generatePieChart3D(HttpSession session, PrintWriter pw, int w, int h, int pollid, int questionid){
		String filename=null;
		PieDataset dataset=getDataSet(pollid,questionid);
		JFreeChart chart=ChartFactory.createPieChart3D(
			"Question"+questionid,
			dataset,
			true,
			false,
			false
		);
		chart.setBackgroundPaint(Color.GRAY);
		try{
			ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
			filename=ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		}catch(IOException e){
			e.printStackTrace();
		}
		return filename;
	}
	   
	public static String generateBarChartVertical(HttpSession session, PrintWriter pw,int w, int h, int pollid, int questionid){   
		String filename = null;   
		CategoryDataset dataset = getDataSet2(pollid, questionid);   
		JFreeChart chart = ChartFactory.createBarChart(   
			"Question"+questionid, 
			"x", 
			"y",    
			dataset, 
			PlotOrientation.VERTICAL,    
			true,  
			false, 
			false 
		);   
		try {   
			ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
			filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);   
			ChartUtilities.writeImageMap(pw, filename, info, true);   
			pw.flush();   
		} catch (IOException e) {   
			e.printStackTrace();   
		}   
		return filename;   
	}
	
	public static String generateBarChart3DVertical(HttpSession session, PrintWriter pw, int w, int h, int pollid, int questionid){
		String filename=null;
		CategoryDataset dataset=getDataSet2(pollid, questionid);
		JFreeChart chart=ChartFactory.createBarChart3D(
				"Question"+questionid, 
				"x", 
				"y", 
				dataset, 
				PlotOrientation.VERTICAL, 
				true, 
				false, 
				false
		);
		try{
			ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
			filename=ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		}catch(IOException e){
			e.printStackTrace();
		}
		return filename;
	}
	
	public static String generateBarChartHorizontal(HttpSession session, PrintWriter pw, int w, int h, int pollid, int questionid){
		String filename=null;
		CategoryDataset dataset=getDataSet2(pollid,questionid);
		JFreeChart chart=ChartFactory.createBarChart(
				"Question"+questionid,
				"x",
				"y",
				dataset,
				PlotOrientation.HORIZONTAL,
				true,
				false,
				false
		);
		try{
			ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
			filename=ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		}catch(IOException e){
			e.printStackTrace();
		}
		return filename;
	}
	
	public static String generateBarChart3DHorizontal(HttpSession session, PrintWriter pw, int w, int h, int pollid, int questionid){
		String filename=null;
		CategoryDataset dataset=getDataSet2(pollid,questionid);
		JFreeChart chart=ChartFactory.createBarChart3D(
				"Question"+questionid,
				"x",
				"y",
				dataset,
				PlotOrientation.HORIZONTAL,
				true,
				false,
				false
		);
		try{
			ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
			filename=ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		}catch(IOException e){
			e.printStackTrace();
		}
		return filename;
	}
	
	private static DefaultPieDataset getDataSet(int pollid,int questionid) {   
		DefaultPieDataset dataset=new DefaultPieDataset();
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@oracle.students.itee.uq.edu.au:1521:iteeo","CSSE3004GE","967431");
			String sql="SELECT Answer.Text,COUNT(*) AS Quantity FROM Answer WHERE Answer.PollID="+pollid+" AND Answer.QuestionID="+questionid+"GROUP BY Answer.Text ORDER BY Answer.Text";
			PreparedStatement pstm=conn.prepareStatement(sql);
			ResultSet rs=pstm.executeQuery(sql);
			while(rs.next()){
				dataset.setValue(rs.getString(1), rs.getInt(2));
			}
		}catch(Exception e){e.printStackTrace();e.getMessage();}
		return dataset; 
	}   

	private static CategoryDataset getDataSet2(int pollid, int questionid) {   
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();   
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@oracle.students.itee.uq.edu.au:1521:iteeo","CSSE3004GE","967431");
			String sql="SELECT Answer.Text,COUNT(*) AS Quantity FROM Answer WHERE Answer.PollID="+pollid+" AND Answer.QuestionID="+questionid+" GROUP BY Answer.Text ORDER BY Answer.Text";

			PreparedStatement ps=conn.prepareStatement(sql);
			ResultSet rs=ps.executeQuery(sql);
			while(rs.next()){
				dataset.addValue(rs.getInt(2), rs.getString(1), "1");
		}
	}catch(Exception e){e.printStackTrace();e.getMessage();}
	return dataset;   
	}   

}  